package com.kingre.et.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * IP
 * 
 * @author Jingui.Yu
 * 
 */
@Entity
@Table(name = "t_ip")
public class IP extends BaseObject implements Serializable {
    private static final long serialVersionUID = 5104016328822890388L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @ManyToOne(targetEntity = School.class)
    @JoinColumn(name = "school_id")
    private School school;

    @Column(name = "from_ip", nullable = false)
    private Long fromIP;

    @Column(name = "end_ip", nullable = false)
    private Long endIP;

    public Integer getId() {
        return id;
    }

    public School getSchool() {
        return school;
    }

    public String getFromIP() {
        int arr[] = { (int) ((fromIP / 1000000000)), (int) ((fromIP / 1000000) % 1000), (int) ((fromIP / 1000) % 1000),
                (int) (fromIP % 1000) };
        return String.valueOf(arr[0]) + "." + String.valueOf(arr[1]) + "." + String.valueOf(arr[2]) + "."
                + String.valueOf(arr[3]);
    }

    public String getEndIP() {
        int arr[] = { (int) ((endIP / 1000000000)), (int) ((endIP / 1000000) % 1000), (int) ((endIP / 1000) % 1000),
                (int) (endIP % 1000) };
        return String.valueOf(arr[0]) + "." + String.valueOf(arr[1]) + "." + String.valueOf(arr[2]) + "."
                + String.valueOf(arr[3]);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof IP)) {
            return false;
        }
        final IP questionnaire = (IP) o;
        return this.hashCode() == questionnaire.hashCode();
    }

    @Override
    public int hashCode() {
        int result;
        result = (id != null ? id.hashCode() : 0);
        result = 29 * result + (school != null ? school.hashCode() : 0);
        result = 29 * result + (fromIP != null ? fromIP.hashCode() : 0);
        result = 29 * result + (endIP != null ? endIP.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append("id", id).append("school", school)
                .append("fromIP", fromIP).append("endIP", endIP.toString()).toString();
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setSchool(School school) {
        this.school = school;
    }

    public void setFromIP(String fromIP) {
        String[] arr = org.apache.commons.lang.StringUtils.split(fromIP, ".");
        this.fromIP = Integer.parseInt(arr[0]) * 1000000000L + Integer.parseInt(arr[1]) * 1000000L
                + Integer.parseInt(arr[2]) * 1000L + Integer.parseInt(arr[3]);
    }

    public void setEndIP(String endIP) {
        String[] arr = org.apache.commons.lang.StringUtils.split(endIP, ".");
        this.endIP = Integer.parseInt(arr[0]) * 1000000000L + Integer.parseInt(arr[1]) * 1000000L
                + Integer.parseInt(arr[2]) * 1000L + Integer.parseInt(arr[3]);
    }

}
